from functools import wraps
from flask import session,redirect,url_for,g
import config

def login_request(func):
    @wraps(func)
    def wrapper(*args,**kwargs):
        if session.get(config.CMS_USER_ID):
            return func(*args,**kwargs)
        else:
            return redirect(url_for("cms.login"))
    return wrapper


def permission_request(permission):
    def request_fun(func):
        @wraps(func)
        def wrapper(*args,**kwargs):
            user = g.cms_user
            if user.has_permission(permission):
                return func(*args,**kwargs)
            else:
                return redirect(url_for("cms.index"))
        return wrapper
    return request_fun